/**
 * 
 */
package co.fitstart.payment.dao;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericJdbcSupport;

import co.fitstart.payment.entity.request.RefundRecord;
import co.fitstart.payment.entity.transaction.TransactionRecord;

/**
 * @author lute
 *
 */
@Repository("refundRecordDaoSupport")
public class RefundRecordDaoSupport {

    @Autowired
    private GenericJdbcSupport genericJdbcSupport;
    
    /**
     * Get refund record by refund number
     * 
     * @param refundNo
     * @return
     */
    public RefundRecord getRefundRecord(String refundNo) {
        String sentence = "SELECT id AS id, app_id AS app_id, channel_id AS channel_id, transaction_record_id AS transaction_record_id, refund_no AS refund_no, refund_id AS refund_id, order_no AS order_no, total_amount AS total_amount, refund_amount AS refund_amount, is_batch AS batch, batch_data AS batch_data, notify_url AS notify_url, refund_time AS refund_time, is_finished AS finished, is_disabled AS disabled FROM refund_record WHERE refund_no = :refundNo AND is_disabled = false";
        List<RefundRecord> refundRecords = genericJdbcSupport.queryForList(sentence, "refundNo", refundNo, RefundRecord.class);
        return CollectionUtils.isEmpty(refundRecords) ? null : refundRecords.get(0);
    }

    /**
     * Get all finished refund records of transaction record
     * 
     * @param transactionRecord
     * @return
     */
    public List<RefundRecord> getFinishedRefundRecord(TransactionRecord transactionRecord) {
        String sentence = "SELECT id AS id, app_id AS app_id, channel_id AS channel_id, transaction_record_id AS transaction_record_id, refund_no AS refund_no, refund_id AS refund_id, order_no AS order_no, total_amount AS total_amount, refund_amount AS refund_amount, is_batch AS batch, batch_data AS batch_data, notify_url AS notify_url, refund_time AS refund_time, is_finished AS finished, is_disabled AS disabled FROM refund_record WHERE transaction_record_id = :transactionRecordId AND is_finished = true AND is_disabled = false";
        return genericJdbcSupport.queryForList(sentence, "transactionRecordId", transactionRecord.getId(), RefundRecord.class);
    }

}
